﻿import flask
from flask import request  #获取参数
import json
import get
import mysql
# import json #post请求传入json对象时，通过json获取参数

def conn_mysql(sql):
    import pymysql
    conn = pymysql.connect(host='127.0.0.1 ', user='jessica', password='123456', db='sql_python', charset='utf8')
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute(sql)
    res = cur.fetchone()
    print(res)
    conn.commit()
    cur.close()
    conn.close()
    return res

server = flask.Flask(__name__) #创建一个flask对象


@server.route('/index', methods=['get','post'])
def gethb():
	try:
		fromUsername=request.values.get('fromUsername')
		lucky = request.values.get('lucky')
		time = request.values.get('time')
		url = request.values.get('url')
		ret=get.elmhb(url,lucky,time)
		if(ret==None):
			mysql.inserthb(time,url,3,fromUsername,lucky)
			res = {'msg':'系统繁忙', 'msg_code': 0}
		elif ret==True:
			mysql.inserthb(time,url,1,fromUsername,lucky)
			res = {'msg':'领取成功', 'msg_code': 1}
		else:
			mysql.inserthb(time,url,2,fromUsername,lucky)
			res = {'msg':'领取失败', 'msg_code': 2}
	except:
		res={'msg':'Error', 'msg_code':-1}
	return json.dumps(res, ensure_ascii=False) #二进制转化为utf-8


server.run(port=443,debug=True,host='0.0.0.0',ssl_context=('Apache/2_www.xianuan.xyz.crt','Apache/3_www.xianuan.xyz.key')) #debug设置为True，修改接口信息后直接刷新接口即可；添加参数host='0.0.0.0'允许同一局域网内访问